package com.wtone.platform.gat.images.gat.core;

import com.google.common.collect.Maps;
import com.wtone.platform.gat.images.gat.model.*;
import com.wtone.platform.gat.images.gat.utils.DateUtils;
import com.wtone.platform.gat.images.gat.utils.JacksonUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: aiLin
 * @Date: 2020/9/27 9:21
 * @desc: 返回对象工具类
 */
public class Response {

    /**
     * 正常响应
     * @param id
     * @param requestURL
     * @return
     */
    public static  ResponseStatusObject ok(String id,String requestURL){
        ResponseStatusObject result = new ResponseStatusObject();
        ResponseStatus r = new ResponseStatus();
        r.setId(id);
        r.setLocalTime(DateUtils.dateTimeNows());
        r.setStatusCode(Constants.SUCCESS);
        r.setStatusString("正常");
        r.setRequestURL(requestURL);
        result.setResponseStatus(r);
        return result;
    }

    /**
     * 正常响应
     * @param id
     * @param requestURL
     * @return
     */
    public static  ResponseStatusObject ok(String id,String requestURL,String successMsg){
        ResponseStatusObject result = new ResponseStatusObject();
        ResponseStatus r = new ResponseStatus();
        r.setId(id);
        r.setLocalTime(DateUtils.dateTimeNows());
        r.setStatusCode(Constants.SUCCESS);
        r.setStatusString(successMsg!=null ? successMsg : "正常");
        r.setRequestURL(requestURL);
        result.setResponseStatus(r);
        return result;
    }
    /**
     * 正常响应
     * @param id
     * @param requestURL
     * @return
     */
    public static ResponseStatus getResponOk(String id,String requestURL){
        ResponseStatus r = new ResponseStatus();
        r.setId(id);
        r.setLocalTime(DateUtils.dateTimeNows());
        r.setStatusCode(Constants.SUCCESS);
        r.setStatusString("正常");
        r.setRequestURL(requestURL);
        return r;
    }

    /**
     * 正常响应
     * @param id
     * @param requestURL
     * @return
     */
    public static ResponseStatus getResponOk(String id,String requestURL,String successMsg){
        ResponseStatus r = new ResponseStatus();
        r.setId(id);
        r.setLocalTime(DateUtils.dateTimeNows());
        r.setStatusCode(Constants.SUCCESS);
        r.setStatusString(successMsg!=null ? successMsg :"正常");
        r.setRequestURL(requestURL);
        return r;
    }

    /**
     * 异常响应
     * @param id
     * @param requestURL
     * @return
     */
    public static ResponseStatus getResponError(String id,String requestURL,String errorMsg){
        ResponseStatus r = new ResponseStatus();
        r.setId(id);
        r.setLocalTime(DateUtils.dateTimeNows());
        r.setStatusCode(Constants.OTHERERROR);
        r.setStatusString(errorMsg);
        r.setRequestURL(requestURL);
        return r;
    }


    /**
     * 正常响应
     * @param id
     * @param requestURL
     * @return
     */
    public static  ResponseStatusObject error(String id,String requestURL){
        ResponseStatusObject result = new ResponseStatusObject();
        ResponseStatus r = new ResponseStatus();
        r.setId(id);
        r.setLocalTime(DateUtils.dateTimeNows());
        r.setStatusCode(Constants.OTHERERROR);
        r.setStatusString("异常");
        r.setRequestURL(requestURL);
        result.setResponseStatus(r);
        return result;
    }

    /**
     * 正常响应
     * @param id
     * @param requestURL
     * @return
     */
    public static  ResponseStatusObject errorMsg(String id,String requestURL,String errorMsg){
        ResponseStatusObject result = new ResponseStatusObject();
        ResponseStatus r = new ResponseStatus();
        r.setId(id);
        r.setLocalTime(DateUtils.dateTimeNows());
        r.setStatusCode(Constants.OTHERERROR);
        r.setStatusString(errorMsg!=null ? errorMsg : "异常");
        r.setRequestURL(requestURL);
        result.setResponseStatus(r);
        return result;
    }

    /***
     * 正常响应
     * @return
     */
    public static ResponseStatusListModeObject ok(List<ResponseStatus> resp3){
        ResponseStatusListModeObject resp = new ResponseStatusListModeObject();
        ResponseStatusListObject resp2 = new ResponseStatusListObject();
        resp2.setResponseStatuses(resp3);
        resp.setResponseStatusListObject(resp2);
        return resp;
    }

    /***
     * 正常响应 订阅对象集合
     * @return
     */
    public static SubscribeListObject okSubList(List<Subscribe> subscribes){
        SubscribeListObject sub = new SubscribeListObject();
        SubscribeObject s = new SubscribeObject();
        s.setSubscribeList(subscribes);
        sub.setSubscribeObject(s);
        return sub;
    }

    /**
     * 错误返回
     * @param id
     * @param requestURL
     * @param code
     * @return
     */
    public static ResponseStatusListModeObject error(String id,String requestURL,String code){

        ResponseStatusListModeObject resp = new ResponseStatusListModeObject();
        ResponseStatusListObject resp2 = new ResponseStatusListObject();
        List<ResponseStatus> resp3 = new ArrayList<ResponseStatus>();

        ResponseStatus r = new ResponseStatus();
        r.setId(id);
        r.setLocalTime(DateUtils.dateTimeNows());
        r.setStatusCode(code!=null ? code : Constants.OTHERERROR);
        r.setStatusString("异常");
        r.setRequestURL(requestURL);
        resp3.add(r);
        resp2.setResponseStatuses(resp3);
        resp.setResponseStatusListObject(resp2);

        return resp;
    }

    /**
     * 错误返回
     * @param id
     * @param requestURL
     * @param code
     * @return
     */
    public static ResponseStatusListModeObject error(String id,String requestURL,String code,String errorMsg){

        ResponseStatusListModeObject resp = new ResponseStatusListModeObject();
        ResponseStatusListObject resp2 = new ResponseStatusListObject();
        List<ResponseStatus> resp3 = new ArrayList<ResponseStatus>();

        ResponseStatus r = new ResponseStatus();
        r.setId(id);
        r.setLocalTime(DateUtils.dateTimeNows());
        r.setStatusCode(code!=null ? code : Constants.OTHERERROR);
        r.setStatusString(errorMsg!=null ? errorMsg : "异常");
        r.setRequestURL(requestURL);
        resp3.add(r);
        resp2.setResponseStatuses(resp3);
        resp.setResponseStatusListObject(resp2);

        return resp;
    }


    /**
     * 对象转json时属性变大写
     *
     * @param entity
     * @return
     */
    public static Map<String,Object> object2UpperCase(Object entity){
        Map<String,Object> res = Maps.newHashMap();
        if (null==entity){
            return res;
        }
        //将对象的属性转化为大写
        String json = JacksonUtil.bean2Json(entity);
        Map<String,Object> map = JacksonUtil.json2Map(json,String.class,Object.class);
        if (null!=map && !map.isEmpty()){
            for(Map.Entry<String,Object> entry:map.entrySet()){
                /**
                 * TODO-关键在这里根据业务需要转大小写
                 */
                if(entry.getKey().contains("SubscribeNotificationListObject")){
                    res.put("SubscribeNotificationListObject",entry.getValue());
                }else if(entry.getKey().contains("RegisterObject")){
                    res.put("RegisterObject",entry.getValue());
                }else{
                    res.put(entry.getKey().toUpperCase(),entry.getValue());
                }
            }
        }
        return res;
    }

}
